local i
local _G = getfenv(0)

-------------------------Wheres all the fonts and textures? Here!

hooksecurefunc("AuraButton_Update", function(buttonName, index, filter)
    local button = _G[buttonName..index]
    if (button) then
        button:SetScale(1)
    end
    
    local icon = _G[buttonName..index.."Icon"]
    if (icon) then
        icon:SetTexCoord(0.03, 0.97, 0.02, 0.97)
    end
    
    local duration = _G[buttonName..index.."Duration"]
    if (duration) then
        duration:ClearAllPoints()
        duration:SetPoint("CENTER", button, "BOTTOM", 0, 3)
        duration:SetFont("Interface\\AddOns\\eMedia\\font.ttf", 14, "OUTLINE")		
		duration:SetShadowOffset(0, 0)
        duration:SetDrawLayer("OVERLAY")
    end
    
    local count = _G[buttonName..index.."Count"]
    if (count) then
        count:ClearAllPoints()
        count:SetPoint("TOPRIGHT", button)
        count:SetFont((count:GetFont()), 17, "OUTLINE")
        count:SetDrawLayer("OVERLAY")
    end
    
   local border = _G[buttonName..index.."Border"]
    if (border) then
        border:SetParent(button)
        border:SetTexture("Interface\\AddOns\\eMedia\\Button")
        border:SetPoint("TOPRIGHT", button, 2, 2)
        border:SetPoint("BOTTOMLEFT", button, -2, -2)
        border:SetTexCoord(0, 1, 0, 1)
    end
    
    local skin = _G[buttonName..index.."Skin"]
    if (button and not skin and not border) then
        local overlay = CreateFrame("Frame", buttonName..index.."Skin", button)
        overlay:SetAllPoints(button)
        overlay:SetParent(button)
        
        local texture = overlay:CreateTexture(nil, "BORDER")
        texture:SetParent(button)
        texture:SetTexture("Interface\\AddOns\\eMedia\\Button")
        texture:SetPoint("TOPRIGHT", overlay, 2, 2)
        texture:SetPoint("BOTTOMLEFT", overlay, -2, -2)
        --texture:SetVertexColor(0.7, 0.6, 0.6)
    end
end)

for i = 1, 2 do 
    local button = _G["TempEnchant"..i]
    button:SetScale(1)
    
    local icon = _G["TempEnchant"..i.."Icon"]
    icon:SetTexCoord(0.03, 0.97, 0.02, 0.97)

    local duration = _G["TempEnchant"..i.."Duration"]
    duration:ClearAllPoints()
    duration:SetPoint("CENTER", button, "BOTTOM", 0, 3)
    duration:SetFont("Interface\\AddOns\\eMedia\\font.ttf", 14, "OUTLINE")	
    duration:SetShadowOffset(0, 0)
    duration:SetDrawLayer("OVERLAY")

    local border = _G["TempEnchant"..i.."Border"]
    border:SetTexture("Interface\\AddOns\\eMedia\\Button")
    border:SetTexCoord(0, 1, 0, 1)
    border:SetVertexColor(0.9, 0.25, 0.9)
    border:ClearAllPoints()
    border:SetPoint("TOPRIGHT", button, 2, 2)
    border:SetPoint("BOTTOMLEFT", button, -2, -2)
end

--------------------------end-------------------

-----------------formats and arrangement------------

BUFFSPACING_X = 10
BUFFSPACING_Y = 14

BUFF_MIN_ALPHA = 0.2
BUFFS_PER_ROW = 12
-- more custom stuff
BUFF_FLASH_TIME_ON = 0.4;
BUFF_FLASH_TIME_OFF = 0.4;

DAY_ONELETTER_ABBR = "%dd"
HOUR_ONELETTER_ABBR = "%dh"
MINUTE_ONELETTER_ABBR = "%dm"
SECOND_ONELETTER_ABBR = "%ds"

-----------------------end-------------------------

TemporaryEnchantFrame:ClearAllPoints()
TemporaryEnchantFrame:SetPoint("TOPRIGHT", UIParent, "TOPRIGHT", -18, -18)
TemporaryEnchantFrame.SetPoint = function() end

TempEnchant2:ClearAllPoints()
TempEnchant2:SetPoint("BOTTOMLEFT", TempEnchant1, "BOTTOMLEFT", -35, 0)

TemporaryEnchantFrame:HookScript("OnUpdate", function()
    local hasMainHandEnchant, _, _, hasOffHandEnchant, _, _ = GetWeaponEnchantInfo()
    if (hasMainHandEnchant and hasOffHandEnchant) then
        BuffFrame:ClearAllPoints()
        BuffFrame:SetPoint("TOPRIGHT", TempEnchant2, "TOPLEFT", -10, 0)
        return
    elseif (hasMainHandEnchant or hasOffHandEnchant) then
        BuffFrame:ClearAllPoints()
        BuffFrame:SetPoint("TOPRIGHT", TempEnchant1, "TOPRIGHT", -60, 0)
        return
    elseif (not hasMainHandEnchant and not hasOffHandEnchant) then
        BuffFrame:ClearAllPoints()
        BuffFrame:SetPoint("TOPRIGHT", TemporaryEnchantFrame, "TOPRIGHT", 0, 0)
        return
	end
end)

--hooksecurefunc("BuffButton_UpdateAnchors", function(buttonName, index)
function BuffButton_UpdateAnchors(buttonName, index)
	local buff = _G[buttonName..index]
    if (index == 1) then
        buff:SetPoint("TOPRIGHT", BuffFrame, "TOPRIGHT", 0, 0)
    elseif (index == 13) then
        buff:SetPoint("TOP", TempEnchant1, "BOTTOM", 0, -BUFFSPACING_Y)
    elseif (index == 25) then
        buff:SetPoint("TOP", BuffButton13, "BOTTOM", 0, -BUFFSPACING_Y)
    elseif (index == 33) then
        buff:SetPoint("TOP", BuffButton25, "BOTTOM", 0, -BUFFSPACING_Y)
    else
        buff:SetPoint("RIGHT", getglobal(buttonName..(index-1)), "LEFT", -BUFFSPACING_X, 0)
    end
end

hooksecurefunc("DebuffButton_UpdateAnchors", function(buttonName, index)
--function DebuffButton_UpdateAnchors(buttonName, index)
	local rows = ceil(BUFF_ACTUAL_DISPLAY/BUFFS_PER_ROW)
	local buff = getglobal(buttonName..index)
	local buffHeight = TempEnchant1:GetHeight()

	if ((index > 1) and (mod(index, BUFFS_PER_ROW) == 1) ) then
		buff:SetPoint("TOP", getglobal(buttonName..(index-BUFFS_PER_ROW)), "BOTTOM", 0, -BUFFSPACING_Y)
	elseif (index == 1) then
		if (rows < 2) then
			buff:SetPoint("TOPRIGHT", TempEnchant1, "BOTTOMRIGHT", 0, -1 * ((2 * BUFFSPACING_Y) + buffHeight))
		else
			buff:SetPoint("TOPRIGHT", TempEnchant1, "BOTTOMRIGHT", 0, -rows * (BUFFSPACING_Y + buffHeight))
		end
	else
		buff:SetPoint("RIGHT", getglobal(buttonName..(index-1)), "LEFT", -BUFFSPACING_X, 0)
	end
end)

